﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using FastestWays.Helpers;
using FastestWays.Model;
using FastestWays.Server;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Phone.Shell;
using WazePocWP8.Server;

namespace FastestWays.ViewModel
{
    public class AddressesSearchViewModel : ViewModelBase
    {
        AddressesEngine _addressesEngine;
        private IMessageHandler _messageHandler = AppServicesLocator.MessageHandler;

        public AddressesSearchViewModel()
        {
            SearchForNewAddressCommand = new RelayCommand(SearchForNewAddress);
            CommitSearchAndGoBackCommand = new RelayCommand<Address>(CommitSearchAndGoBack);

            _addressesEngine = new AddressesEngine();
            _addressesEngine.AddressesInSearch.CollectionChanged += AddressesInSearchCollectionChanged;

            CreateAllAddresses();

            PhoneApplicationService.Current.Closing += CurrentClosing;
        }

        private void CurrentClosing(object sender, ClosingEventArgs e)
        {
            _addressesEngine.SaveAddressListToIsoStorage(HistoryAddresses.ToList());
        }

        private void CommitSearchAndGoBack(Address address)
        {
            HistoryAddresses.Add(address);
            Parameters.Coordinate = address.Geocoordinate;
            Parameters.CurrentCountry = address.CurrentCountry;
            Messenger.Default.Send(new MessageBase());
        }

        private void AddressesInSearchCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (_addressesEngine.AddressesInSearch.Any())
            {
                if (_addressesEngine.AddressesInSearch.FirstOrDefault() is FakeAddress)
                {
                    _messageHandler.DisplayMessage("No addresses found for this search, please try again with a diffrent phrase");
                }
                else
                {
                    OptionaryAddresses = _addressesEngine.AddressesInSearch;
                }
            }
        }


        private void SearchForNewAddress()
        {
            _addressesEngine.GetAddressesList(_addressText);
        }

        private void CreateAllAddresses()
        {
            HistoryAddresses = new ObservableCollection<Address>(_addressesEngine.LoadAddressListFromIsoStorage());
        }


        #region Commands

        public RelayCommand SearchForNewAddressCommand
        {
            get;
            private set;
        }

        public RelayCommand<Address> CommitSearchAndGoBackCommand
        {
            get;
            private set;
        }
        #endregion

        #region Properties

        private string _addressText;
        public string AddressText
        {
            get { return _addressText; }
            set
            {
                _addressText = value;
                RaisePropertyChanged("AddressText");
            }
        }

        public ObservableCollection<Address> HistoryAddresses
        {
            get { return _historyAddresses; }
            set
            {
                _historyAddresses = value;
                RaisePropertyChanged("HistoryAddresses");
            }
        }

        private ObservableCollection<Address> _optionaryAddresses;
        private ObservableCollection<Address> _historyAddresses;

        public ObservableCollection<Address> OptionaryAddresses
        {
            get { return _optionaryAddresses; }
            set
            {
                _optionaryAddresses = value;
                RaisePropertyChanged("OptionaryAddresses");
            }
        }


        #endregion
    }
}
